package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import dto.MItem;
import pojo.Food;
import pojo.FoodType;
import service.FoodListManager;
import service.factory.FoodListManagerFactory;
import util.JacksonUtil;

/**
 * Servlet implementation class RequestFoodListServlet
 */
public class RequestFoodListServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	private FoodListManager manager;

	public RequestFoodListServlet() {
		super();
		this.manager = FoodListManagerFactory.getInstance();
	}

	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		String mid = req.getParameter("mid");
		String curtype = req.getParameter("curtype");
		String mname = req.getParameter("curname");
        System.out.println("foodListServlet");
		// 从session中获取商家列表
		List<MItem> mlist = (List<MItem>) req.getSession()
				.getAttribute("mlist");

		boolean found = false;
		if (mlist != null)
			// for(MItem item : mlist){
			// if(item.getId().equals(mid)){
			// found = true;
			// }
			// }
			for (int i = 0; i < mlist.size(); i++) {
				MItem it = mlist.get(i);
				if (it.getId().equals(mid)) {
					found = true;
				}
			}
		// 当前商家在商家列表中找不到，说明该商家被拉黑
		if (!found) {
			resp.sendRedirect("/ceat/mNotFound.html");
			return;
		}

		if (curtype == null || curtype.trim().length() == 0)
			curtype = "1";
		Logger.getLogger("FILE").info(
				"curmid = " + mid + ", curtype = " + curtype);
		Logger.getLogger("FILE").info(
				"访客查看" + mid + "的店铺， curtype=" + curtype);

		// List<FoodType> foodType = this.manager.loadFoodType();
		List<Food> foodList = this.manager.loadFoodList(mid, curtype);

		// req.setAttribute("foodtypes", foodType);
		req.setAttribute("foodlist", foodList);
		req.setAttribute("curmid", mid);
		req.setAttribute("curname", mname);
		req.setAttribute("curtype", curtype);
		req.getRequestDispatcher("sec/foods.jsp").forward(req, resp);
	}
	
	
	public void judgeIfExist(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		System.out.println("调用了judgeIfExist()");
		///html 的请求
		String mid = req.getParameter("mid");
		if(mid==null){
			mid = (String) req.getAttribute("mid");
		}
		
		System.out.println("judgeIfExist 中获取到mid：" +mid);
		
		List<MItem> mlist = (List<MItem>) req.getSession().getAttribute(
				"mlist");
		boolean flag = false;
		for(MItem item:mlist){
			if(item.getId().equals(mid)){
				flag = true;
				break;
			}
		}
		if(flag){
			//此用户还没被拉黑
			// 转回JSON串
			// 发送回给客户端
			PrintWriter writer = resp.getWriter();
			List<Food> foodList = this.manager.findAllFoods(mid);
			if(foodList==null){
				foodList = new ArrayList<Food>();
			}
			String jsonStr = JacksonUtil.fromObjectToJson(foodList);
			System.out.println(jsonStr);
			writer.write(jsonStr);
			writer.close();
		}else{
			//返回一个空值给客户端
//			PrintWriter writer = resp.getWriter();
//			writer.write("][");///回传一个错误的json串给客户端
//			writer.close();
			resp.sendRedirect("/ceat/mNotFound.html");
			return;
		}

		
	}
	
}
